package com.yf.zzmusic.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yf.zzmusic.common.Constants;
import com.yf.zzmusic.common.PageInfo;
import com.yf.zzmusic.exception.ServiceException;
import com.yf.zzmusic.pojo.dto.user.UserLoginDTO;
import com.yf.zzmusic.pojo.dto.user.UserPageDTO;
import com.yf.zzmusic.pojo.dto.user.UserTypeDTO;
import com.yf.zzmusic.pojo.dto.user.UserUpdateDTO;
import com.yf.zzmusic.pojo.entity.User;
import com.yf.zzmusic.mapper.UserMapper;
import com.yf.zzmusic.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.zzmusic.utils.Base64Util;
import com.yf.zzmusic.utils.EchartsUtils;
import com.yf.zzmusic.utils.FreemarkerUtil;
import org.springframework.beans.factory.annotation.Value;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;


/**
 * <p>
 * 用户表，用户分为普通用户，歌手与管理员 服务实现类
 * </p>
 *
 * @author
 * @since 2024-02-03
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
  // PhontomJS 服务网址
//  @Value("${phantomjs.url}")
  private String phantomjsUrl="http://127.0.0.1:9090";

  @Override
  public User login(UserLoginDTO userLoginDTO) {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("phone", userLoginDTO.getPhone());
    queryWrapper.eq("password", userLoginDTO.getPassword());
    User user;
    try {
      user = getOne(queryWrapper);
    } catch (Exception e) {
      throw new ServiceException(Constants.CODE_ERROR, "系统错误");
    }
    if (user != null) {
      return user;
    } else {
      throw new ServiceException(Constants.NO_CONTENT, "账号或密码不正确");
    }
  }

  @Override
  public boolean updateUser(UserUpdateDTO userUpdateDTO) {
    if (getById(userUpdateDTO.getUserId())==null){
      throw new ServiceException(Constants.NO_CONTENT,"用户未找到");
    }
    User user=new User();
    //拷贝
    BeanUtils.copyProperties(userUpdateDTO,user);
    //调用mybatis的方法进行更新
    return updateById(user);
  }


  @Override
  public PageInfo userByPage(UserPageDTO userPageDTO) {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    Integer pageNum = userPageDTO.getPageNum();
    Integer pageSize = userPageDTO.getPageSize();
    String phone = userPageDTO.getPhone();
    String nickname = userPageDTO.getNickname();
    Integer type = userPageDTO.getType();
    String realName = userPageDTO.getRealName();
    Integer nationalityId = userPageDTO.getNationalityId();
    //判断pageNum与pageSize
    if(pageNum==null){
      pageNum=0;
    }
    if(pageSize==null){
      pageSize=1000;
    }
    if (!StringUtils.isEmpty(phone)) {
      queryWrapper.like("phone", phone);
    }
    if (!StringUtils.isEmpty(nickname)) {
      queryWrapper.like("nickname", nickname);
    }
    if (type!=null) {
      queryWrapper.eq("type", type);
    }
    if (!StringUtils.isEmpty(realName)) {
      queryWrapper.like("real_name", realName);
    }
    if (nationalityId!=null) {
      queryWrapper.eq("nationality_id", nationalityId);
    }
//    queryWrapper.orderByAsc("id");//排序方式
    Page<User> userPage = page(new Page<>(pageNum, pageSize), queryWrapper);
    PageInfo<User> pageInfo=new PageInfo<>();
    pageInfo.setPageNum(pageNum);
    pageInfo.setPageSize(pageSize);
    pageInfo.setTotal(userPage.getTotal());
    pageInfo.setData(userPage.getRecords());
    return pageInfo;
  }

  /**
   * 根据用户id修改用户类型 0表示普通用户 1表示歌手 2表示管理员
   * @return
   */
  @Override
  public boolean updateTypeByUserId(UserTypeDTO userTypeDTO) {
    if (getById(userTypeDTO.getUserId())==null){
      throw new ServiceException(Constants.NO_CONTENT,"用户未找到");
    }
    User user=new User();
    user.setUserId(userTypeDTO.getUserId());
    user.setType(userTypeDTO.getType());
    return updateById(user);
  }

  @Override
  public File generateEcharts() {
    // 数据参数，可以自己通过API查询json数据
    String title = "上海天气折线图";
    List<String> categories = Arrays.asList("2022-07-10", "2022-07-11", "2022-07-12", "2022-07-13", "2022-07-14", "2022-07-15", "2022-07-16", "2022-07-17", "2022-07-18", "2022-07-19", "2022-07-20", "2022-07-21", "2022-07-22");
    List<String> values = Arrays.asList("38", "33", "33", "31", "30", "32", "34", "37", "38", "37", "36", "38", "37");
    // 模板参数
    HashMap<String, Object> data = new HashMap<>();
    data.put("title", title);
    data.put("categories", JSON.toJSONString(categories));
    data.put("values", JSON.toJSONString(values));
    // 调用模板加载数据
    String option = FreemarkerUtil.generate("EChartsLineOption.ftl", data);
    // 生成图片的base64编码
    String base64 = EchartsUtils.generateEChartsBase64(phantomjsUrl, option);
    // 将base64转为文件
    return Base64Util.base64ToFile(base64);
  }
}
